package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.HttpGetUtils;
import org.apache.http.client.methods.HttpGet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private JedisPool jedisPool; //redis连接池

    @Override
    public void add(Address address) {
        //从对象中address取出机构名称
        String name = address.getName();
        //通过工具类获取uid
        String uid = HttpGetUtils.getUId(name);
        System.out.println("_______________________________"+uid);
        //向对象中添加uid
        address.setUid(uid);
        //获取地址
        String address1 = address.getAddress();
        //取出双引号
        String jsonString = jsonString(address1);
        //重新添加到集合中
        address.setAddress(jsonString);
        //dao层调用添加方法
        addressDao.add(address);
        //获取地址ID信息
        Integer addressId = address.getId();
        //获取地址名称
        String addressName = address.getName();
        //存取到redis集合中去
        jedisPool.getResource().sadd(RedisConstant.ADDRESS_PIC_DB_RESOURCES, addressName);
        //setOrderAndAddress(checkGroup.getId(),checkitemIds);
    }

    /*去除双引号方法*/
    private static String jsonString(String s) {
        return s.replaceAll("\"", "");
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        Page<Address> page = addressDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Address findById(Integer id) {
        return addressDao.findById(id);
    }

    @Override
    public void update(Address address) {
        String address1 = address.getAddress();
        String jsonString = jsonString(address1);
        address.setAddress(jsonString);
        addressDao.update(address);
    }

    @Override
    public void delete(Integer id) {
        //判断此地址是否有套餐关联
        Integer count = addressDao.findOrderIdByAddressId(id);
        if (count > 0) {
            throw new RuntimeException("当前项被引用,不能删除");
        }
        addressDao.delete(id);
    }

    @Override
    public List<Address> findAddress() {
        return addressDao.findAddress();
    }

    @Override
    public String getUid(Integer id) {
        System.out.println(id);
        return addressDao.getUid(id);
    }

}
